/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.pconline.autoclub.service;

import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.ClubMedal;
import cn.pconline.autoclub.entity.ClubMedalRelation;
import cn.pconline.autoclub.util.Pager;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.gelivable.dao.GeliDao;
import org.gelivable.dao.GeliOrm;
import org.gelivable.dao.Mid;
import org.gelivable.dao.SqlBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;

/**
 *
 * @author ppc
 */
public class ClubMedalService {

    @Autowired
    private GeliDao geliDao;
    @Autowired
    private GeliOrm geliOrm;
    @Autowired
    private PagerService pagerService;

    /**
     * 创建勋章
     *
     * @param clubMedal
     * @return
     */
    public long createClubMedal(ClubMedal clubMedal) {
        Date now = new Date();
        clubMedal.setCreateAt(now);
        clubMedal.setUpdateAt(now);
        return geliDao.create(clubMedal);
    }

    /**
     * 更新勋章
     *
     * @param clubMedal
     * @return
     */
    public int updateClubMedal(ClubMedal clubMedal) {
        return geliDao.update(clubMedal);
    }

    public ClubMedal findClubMedalById(long medalId) {
        try {
            ClubMedal clubMedal = geliDao.find(ClubMedal.class, medalId);
            return clubMedal;
        } catch (EmptyResultDataAccessException ex) {
        }
        return null;
    }

    /**
     * 颁发车友会勋章
     *
     * @param clubMedalRelation
     * @return
     */
    public long issueClubMedal(ClubMedalRelation clubMedalRelation) {
        clubMedalRelation.setCreateAt(new Date());
        clubMedalRelation.setUpdateAt(new Date());
        return geliDao.create(clubMedalRelation);
    }

    /**
     * 查询车友会勋章列表(分页)
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Pager<ClubMedal> listClubMedal(int pageNo, int pageSize) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select medal_id from ").appendSql(geliOrm.getTableName(ClubMedal.class));
        return pagerService.pager(ClubMedal.class, sql, pageNo, pageSize);
    }
    
    /**
     * 获取所有勋章列表
     * @return 
     */
    public List<ClubMedal> listClubMedal(){
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select medal_id from ").appendSql(geliOrm.getTableName(ClubMedal.class));
        return geliDao.list(ClubMedal.class, sql.getSql(),sql.getValues());
    }
    /**
     * 查询获取指定勋章的车友会列表
     *
     * @param medalId
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Pager<Club> listClubByMedal(long medalId, int pageNo, int pageSize) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select club_id from ").appendSql(geliOrm.getTableName(ClubAwardRecordService.class)).appendSql(" where medal_id = ").appendValue(medalId);
        return pagerService.pager(Club.class, sql, pageNo, pageSize);
    }
    
    /**
     * 获取车友会勋章
     * @param clubId
     * @param medalId
     * @return 
     */
    public ClubMedalRelation getClubMedalRelation(long clubId,long medalId){
        return geliDao.find(ClubMedalRelation.class, new Mid(clubId,medalId));
    }
    
    /**
     * 查询车友会获得的勋章
     *
     * @param clubId
     * @return
     */
    public List<ClubMedal> listClubMedalByClubId(long clubId) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select medal_id from ").appendSql(geliOrm.getTableName(ClubMedalRelation.class)).appendSql(" where club_id=").appendValue(clubId).appendSql(" order by create_at desc");
        return geliDao.list(ClubMedal.class, sql.getSql(), sql.getValues());
    }
    /**
     * 返回两个集合的差集
     * @param list
     * @param clubMedals
     * @return 
     */
    public List<ClubMedal> removeHasMedal(List<ClubMedal> list, List<ClubMedal> clubMedals) {
          list = new ArrayList<ClubMedal>(list); 
        if (clubMedals != null) {
           Iterator<ClubMedal> it = list.iterator();
           while(it.hasNext()){
               ClubMedal c = it.next();
                  for(ClubMedal clubMedal : clubMedals){
                      if(c.getMedalId() == clubMedal.getMedalId()){
                         it.remove();
                      }
                  }
              }
        }
        return list;
    }
    /**
     * 取消车友会勋章
     * @param relation
     * @return 
     */
    public int cancelMedal(ClubMedalRelation relation){
        return geliDao.delete(relation,new Mid(relation.getClubId(),relation.getMedalId()));
        
    }
    public int deleteMedalRelation(long medalId){
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("delete from ").appendSql(geliOrm.getTableName(ClubMedalRelation.class)).appendSql(" where medal_id=").appendValue(medalId);
        return geliDao.getJdbcTemplate().update(sql.getSql(),sql.getValues());
    }
    /**
     * 删除勋章
     */
    public int deleteMedal(ClubMedal clubMedal){
        if(clubMedal == null) return 0;
        //删除关系表数据
        deleteMedalRelation(clubMedal.getMedalId());
        return geliDao.delete(clubMedal, clubMedal.getMedalId());
    }
}
